package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    private final WmChannelMapper wmChannelMapper;

    private final WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult findAll() {
        log.info("查询所有频道");
        // 1、返回结果
        return ResponseResult.okResult(list());
    }

    /**
     * 频道名称模糊分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listChannelByPage(ChannelDto dto) {
        // 1、检查参数
        if (dto == null || dto.getPage() == null || dto.getSize() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2、分页查询
        dto.setPage((dto.getPage() - 1) * dto.getSize());
        List<AdChannel> wmChannels = wmChannelMapper.listChannelByPage(dto);
        return ResponseResult.okResult(wmChannels);
    }

    /**
     * 根据频道id删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannelById(Integer id) {
        // 1、检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel=wmChannelMapper.selectChannelById(id);
        if(adChannel.getStatus()){ // 频道已启用 不能删除
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_HAS_ENABLE);
        }
        wmChannelMapper.deleteSensitiveById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 保存频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getIsDefault() == null) {
            adChannel.setIsDefault(true);
        }
        if (adChannel.getStatus() == null) {
            adChannel.setStatus(true);
        }
        if (adChannel.getOrd() == null) {
            adChannel.setOrd(0);
        }
        // 频道不能重复
        AdChannel channel = wmChannelMapper.selectChannelByName(adChannel.getName());
        if (channel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        adChannel.setCreatedTime(new Date());
        wmChannelMapper.saveChannel(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 修改频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        if (adChannel == null || StringUtils.isBlank(adChannel.getName()) || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 1、根据名字查询数据库中的频道
        AdChannel channel = wmChannelMapper.selectChannelByName(adChannel.getName());
        if (channel != null) {
            if (!channel.getId().equals(adChannel.getId())) {
                // 不相同 数据库中里有其他频道 与 修改的名字重复
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }
        // 频道相同 或者 数据库中无这个频道的name
        if (adChannel.getStatus() == null) {
            adChannel.setStatus(true);
        }
        // 频道被引用则不能禁用
        if (!adChannel.getStatus()) {
            // 查询文章对该频道的引用 如果被引用 返回错误信息
            int count = wmNewsMapper.selectByChannelId(adChannel.getId());
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_HAS_BEEN_REFERENCE);
            }
        }
        if (adChannel.getIsDefault() == null) {
            adChannel.setIsDefault(true);
        }
        if (adChannel.getOrd() == null) {
            adChannel.setOrd(0);
        }
        wmChannelMapper.updateChannel(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }
}
